package com.yxhl.zoume.core.user.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.UIConstants;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.func.webpage.info.WebPageEntrance;
import com.yxhl.zoume.core.user.ui.activity.UserContainerActivity;
import com.yxhl.zoume.utils.AppInfoUtils;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class AboutUsFragment extends BaseFragment {

    @BindView(R.id.tv_fragment_about_us_version)
    TextView mVersionCodeTv;
    @BindView(R.id.tv_fragment_about_us_html)
    TextView mContentTv;

    //arguments
    private WebPageEntrance mEntrance;


    public AboutUsFragment() {
        // Required empty public constructor
    }

    public static AboutUsFragment newInstance(WebPageEntrance type) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(UIConstants.UserContainer.ARGUMENT_KEY_ENTRANCE_TYPE, type);
        AboutUsFragment fragment = new AboutUsFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        handleArguments();
        initializeShow();
        return view;
    }

    private void handleArguments() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mEntrance = (WebPageEntrance) arguments.getSerializable(UIConstants.UserContainer.ARGUMENT_KEY_ENTRANCE_TYPE);
        }
    }

    private void initializeShow() {
        String versionName = AppInfoUtils.getVersionName();
        mVersionCodeTv.setText("v" + versionName);

        initLocalHTML();
    }


    /**
     * 加载指定本地页面
     */
    private void initLocalHTML() {
        if (null != mEntrance) {
            InputStream is = this.getResources().openRawResource(mEntrance.getContentRes());
            try {
                DataInputStream dis = new DataInputStream(is);
                byte[] buffer = new byte[is.available()];
                dis.readFully(buffer);
                mContentTv.setText(Html.fromHtml(new String(buffer)));
                dis.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    protected void initializeInjector() {
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_about_us;
    }

    @Override
    protected void setupToolBar() {
        ((UserContainerActivity) mActivity).setToolbarTitle(getString(R.string.user_center_about_us));
    }

}
